<?php
/**
 * 提供给Flash前端的接口
 * 需要提供inuId进行验证 
 */
$startTime = microtime( true );
$err = isset( $_GET['err'] )  ? ( E_ALL ^ E_NOTICE ) : 0;
error_reporting( $err );
define( 'DEBUG' , true );
define( 'IN_INU' , true );
define( "ROOT_DIR" , dirname( dirname( dirname( __FILE__ ) ) ) );
define( "MOD_DIR" , ROOT_DIR ."/Model" );
define( "CON_DIR" , ROOT_DIR ."/Controller/Api" );
define( "TPL_DIR" , ROOT_DIR ."/Tpl" );
define( "CONFIG_DIR" , ROOT_DIR . "/Config" );
include MOD_DIR .'/Common.php';

//注册自动加载函数
Common::registerAutoLoad();
if( DEBUG && !empty( $_GET['uId'] ) )
{
	$uId = intval( $_GET['uId'] );
	ini_set('display_errors', 'on');
}
else
{
	if( empty( $_GET['inuId'] ) )
	{
		echo json_encode( array( 'result' => 102 , 'msg' => 'Session Error' ) );
		return ;
	}

	//获取session信息
	$cache = & Common::getCache( 'session' );
	$inuSession = $cache->get( intval( $_GET['inuId']  ) . '_session' );
	if( !$inuSession )
	{
		echo json_encode( array( 'result' => 101 , 'msg' => 'Session Error' ) );
		return ;
	}

	$inuSession = json_decode( $inuSession , true );
	if( !is_array( $inuSession ) )
	{
		echo json_encode( array( 'result' => 101 , 'msg' => 'Session Error' ) );
		return ;
	}

	//校验session校验码
	if( $inuSession["inuId"] != $_GET['inuId'] )
	{
		echo json_encode( array( 'result' => 102 , 'msg' => 'Session Error' ) );
		return ;
	}

	foreach ( $inuSession as $key => $value )
	{
		$_GET[$key] = $value;
	}

	$uId = intval( $inuSession['uId'] );	//当前用户
}
$_POST = json_decode( $_POST['data'] , TRUE );
if( !is_array( $_POST ) )
{
	$_POST = array();
	
	if( DEBUG && $_GET['data'] )
	{
		$_POST = json_decode( $_GET['data'] , true );
	}
}
if( !empty( $_GET['method'] ) && $uId > 0 )
{
	$method = explode( '.' , $_GET['method'] );
	$con = ucfirst( strtolower( $method[0] ) ) . 'Controller';
	$act = $method[1];
	if( file_exists( CON_DIR . "/{$con}.php" ) )
	{
		$conObject = new $con;
		if( method_exists( $conObject , $act ) )
		{
			$conObject->setUser( $uId );
			try
			{
				$info = $conObject->$act();
				$result = 0;
				$msg = '';
			}
			catch ( Exception $e )
			{
				$info = array();
				$result = $e->getCode();
				$msg = $e->getMessage();
			}
			
			$result = array
			(
				'result' => $result ,
				'method' => $_GET['method'] ,
				'msg' => $msg ,
				'info' => $info ,
			);

//			if( isset( $_POST['requireInit'] ) )
//			{
//				$result['info']['init'] = array
//				(
//					'uId' => $uId ,
//					'serverTime' => $_SERVER['REQUEST_TIME'] ,
//				);
//			}

			$endTime = microtime( true );
			if( DEBUG && isset( $_GET["debug"] ) )
			{
				echo "<pre>";print_r( $result );printf( "\n%dus.\n" , ( $endTime - $startTime ) * 1000000 );echo Helper_RunLog::getInstance()->getRunData();echo "</pre>";
			}
			echo json_encode( $result );
			$traceTime = ( int )( ( $endTime - $startTime ) * 1000 );
			$methodConfig = Common::getConfig( 'MethodCode' );
			if ( isset( $methodConfig[ $_GET[ 'method' ] ] ) )
			{
				FM_Accumulator::getInstance( $uId )->action( FM_Accumulator::ACTION_API_TRACE_TIME , $methodConfig[ $_GET[ 'method' ] ] , 0 , $traceTime );
			}
			return ;
		}
	}
}
//print_r($_GET);
echo json_encode( array( 'result' => 3 , 'msg' => 'method not exist' ) );